/********************************************************
 * Filename: NewTclFilePage.java
 * Author: LMU
 * Program: TUnit
 * Description: Creates the new tcl file page.  
 * Revision History:
 * 20060522: Initial Revision.
 * 20061008: Did a little refactoring.  
 * 20061014: Updated to create file with .tcl extension.  
 * *****************************************************/

package edu.lmu.cs.tUnit.wizard;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.ContainerSelectionDialog;

/**
 * Creates a page for creating a new tcl file
 * @author LMU
 */
public class NewTclFilePage extends WizardPage {
	private Text containerText;
	private Text fileNameText;
	private Text authorNameText;
	private Text paramsListText;
	private Text versionText; 
	private ISelection selection;

	/**
	 * Constructor for SampleNewWizardPage.
	 * 
	 * @param pageName
	 */
	public NewTclFilePage(ISelection selection) {
		super("wizardPage");
		setTitle("Create New Tcl File");
		setDescription("This wizard creates a new Tcl File.  " +
				"An initial procedure is created with the same name as the file with the given parameters.  ");
		this.selection = selection;
	}

	/**
	 * @see IDialogPage#createControl(Composite)
	 */
	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		container.setLayout(layout);
		layout.numColumns = 3;
		layout.verticalSpacing = 9;
		
		createPackageControl(container);
		fileNameText = createFileControl(container, "&File name:");
		paramsListText = createTextBox(container, "&Procedure Parameters:");
		versionText = createTextBox(container, "&Version:");
		authorNameText = createTextBox(container, "&Author name:");
		
		initialize();
		dialogChanged();
		setControl(container);
	}
	
	/**
	 * Creates file text box
	 * @param container the container to be used
	 */
	private Text createFileControl(Composite container, String setting) {
		Label label;
		GridData gd;
		Text nameText; 
		label = new Label(container, SWT.NULL);
		label.setText(setting);
		
		nameText = new Text(container, SWT.BORDER | SWT.SINGLE);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		nameText.setLayoutData(gd);
		nameText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		
		label = new Label(container, SWT.NULL);
		label.setText(".tcl");
		return nameText;
	}
	
	/**
	 * Creates author text box
	 * @param container the container to be used
	 */
	private Text createTextBox(Composite container, String setting) {
		Label label;
		GridData gd;
		Text nameText; 
		label = new Label(container, SWT.NULL);
		label.setText(setting);
		
		nameText = new Text(container, SWT.BORDER | SWT.SINGLE);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		nameText.setLayoutData(gd);
		nameText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		
		label = new Label(container, SWT.NULL);
		label.setText("");
		return nameText;
	}

	/**
	 * Creates the package text box
	 * @param container the container to be used
	 */
	private void createPackageControl(Composite container) {
		Label label = new Label(container, SWT.NULL);
		label.setText("&Tcl Package:");

		containerText = new Text(container, SWT.BORDER | SWT.SINGLE);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		containerText.setLayoutData(gd);
		containerText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});

		Button button = new Button(container, SWT.PUSH);
		button.setText("Browse...");
		button.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				handleBrowse();
			}
		});
	}

	/**
	 * Tests if the current workbench selection is a suitable container to use.
	 */
	private void initialize() {
		if (selection != null && selection.isEmpty() == false
				&& selection instanceof IStructuredSelection) {
			IStructuredSelection ssel = (IStructuredSelection) selection;
			if (ssel.size() > 1)
				return;
			Object obj = ssel.getFirstElement();
			if (obj instanceof IResource) {
				IContainer container;
				if (obj instanceof IContainer)
					container = (IContainer) obj;
				else
					container = ((IResource) obj).getParent();
				containerText.setText(container.getFullPath().toString());
			}
		}
	}

	/**
	 * Uses the standard container selection dialog to choose the new value for
	 * the container field.
	 */
	private void handleBrowse() {
		ContainerSelectionDialog dialog = new ContainerSelectionDialog(
				getShell(), ResourcesPlugin.getWorkspace().getRoot(), false,
				"Select Tcl Package");
		if (dialog.open() == ContainerSelectionDialog.OK) {
			Object[] result = dialog.getResult();
			if (result.length == 1) {
				containerText.setText(((Path) result[0]).toString());
			}
		}
	}

	/**
	 * Ensures that the text fields are set.
	 */
	private void dialogChanged() {
		IResource container = ResourcesPlugin.getWorkspace().getRoot()
				.findMember(new Path(getContainerName()));
		String fileName = getFileName();

		if (getContainerName().length() == 0) {
			updateStatus("Tcl Package must be specified");
			return;
		}
		
		if (container == null || (container.getType() & (IResource.PROJECT | IResource.FOLDER)) == 0) {
			updateStatus("Tcl Package must exist");
			return;
		}
		
		if (!container.isAccessible()) {
			updateStatus("Tcl Package  must be writable");
			return;
		}
		
		if (fileName.length() == 0) {
			updateStatus("Tcl File name must be specified");
			return;
		}
		
		if (fileName.replace('\\', '/').indexOf('/', 1) > 0) {
			updateStatus("File name must be valid");
			return;
		}
	
		updateStatus(null);
	}

	private void updateStatus(String message) {
		setErrorMessage(message);
		setPageComplete(message == null);
	}

	// The getter methods
	public String getContainerName() {
		return containerText.getText();
	}

	public String getFileName() {
		return fileNameText.getText();
	}
	
	public String getAuthorName() {
		return authorNameText.getText();
	}
	
	public String getParamsList() {
		return paramsListText.getText();
	}
	
	public String getVersion() {
		return versionText.getText();
	}
}
